﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class DuctAccessoryForm : System.Windows.Forms.Form
	{
		[DllImport("User32.dll")]
		private static extern int SendMessage(int Handle, int wMsg, int wParam, int lParam);

		public static void SetListViewSpacing(ListView lst, int x, int y)
		{
			DuctAccessoryForm.SendMessage(lst.Handle.ToInt32(), 4149, 0, x * 65536 + y);
		}

		public DuctAccessoryForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public DuctAccessoryForm(TreeView tv, Autodesk.Revit.DB.Document doc, string accessoryType, string chooseType, string categoryName)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			if (accessoryType == "duct")
			{
				this.OrderDuctValve(tv);
			}
			else
			{
				tv.Sort();
			}
			this.sfdr = new SpaceFamilyDataReader();
			this.accessoryType = accessoryType;
			this.treeView = tv;
			this.treeView1.Controls.Add(this.treeView);
			this.initTreeView(this.treeView);
			this.initlsvDetail(this.treeView);
			this.chooseType = chooseType;
			this.categoryName = categoryName;
			this.btnMore.Visible = false;
			if (chooseType == "ConnectionSetting")
			{
				this.btnOk.Hide();
				base.AcceptButton = this.btnChoose;
			}
			else
			{
				this.btnChoose.Hide();
				base.AcceptButton = this.btnOk;
			}
			this.initFamilyTypeCB(this.treeView);
			if (accessoryType == "duct")
			{
				this.Text = "风阀布置";
				this.groupBox1.Text = "风阀名称列表";
				this.groupBox2.Text = "风阀图示列表";
				this.btnMore.Text = "更多风阀";
				this.hygrpList.Text = "风阀名称列表";
				this.hygrpDetail.Text = "风阀图示列表";
				this.tvwCurrPrj.Nodes.Add("风管附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_DuctAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_DuctAccessory));
			}
			else if (accessoryType == "pipe" && chooseType == "ConnectionSetting")
			{
				this.Text = "选择水管阀件";
				this.groupBox1.Text = "水阀名称列表";
				this.groupBox2.Text = "水阀图示列表";
				this.btnMore.Text = "更多水阀";
				this.hygrpList.Text = "水阀名称列表";
				this.hygrpDetail.Text = "水阀图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			else if (accessoryType == "pipe")
			{
				this.Text = "水阀布置";
				this.groupBox1.Text = "水阀名称列表";
				this.groupBox2.Text = "水阀图示列表";
				this.btnMore.Text = "更多水阀";
				this.hygrpList.Text = "水阀名称列表";
				this.hygrpDetail.Text = "水阀图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			else if (accessoryType == "pump")
			{
				this.Text = "选择水管阀件";
				this.groupBox1.Text = "水阀名称列表";
				this.groupBox2.Text = "水阀图示列表";
				this.btnMore.Text = "更多水阀";
				this.hygrpList.Text = "水阀名称列表";
				this.hygrpDetail.Text = "水阀图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			else if (accessoryType == "PipeAccessories")
			{
				this.Text = "管道附件布置";
				this.groupBox1.Text = "管道附件名称列表";
				this.groupBox2.Text = "管道附件图示列表";
				this.btnMore.Text = "更多附件";
				this.hygrpList.Text = "管道附件名称列表";
				this.hygrpDetail.Text = "管道附件图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			else if (accessoryType == "GPipeAccessories")
			{
				this.Text = "给水附件布置";
				this.groupBox1.Text = "给水附件名称列表";
				this.groupBox2.Text = "给水附件图示列表";
				this.btnMore.Text = "更多附件";
				this.hygrpList.Text = "给水附件名称列表";
				this.hygrpDetail.Text = "给水附件图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			else if (accessoryType == "PPipeAccessories")
			{
				this.Text = "排水附件布置";
				this.groupBox1.Text = "排水附件名称列表";
				this.groupBox2.Text = "排水附件图示列表";
				this.btnMore.Text = "更多附件";
				this.hygrpList.Text = "排水附件名称列表";
				this.hygrpDetail.Text = "排水附件图示列表";
				this.tvwCurrPrj.Nodes.Add("管道附件");
				this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
			}
			this.InitlsvCurrPrjDetail(this.tvwCurrPrj);
			this.InitTabCotorl();
		}

		public DuctAccessoryForm(TreeView tv, Autodesk.Revit.DB.Document doc, string accessoryType, string chooseType, string categoryName, bool beSelect)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.seclected = beSelect;
			if (accessoryType == "duct")
			{
				this.OrderDuctValve(tv);
			}
			else
			{
				tv.Sort();
			}
			this.sfdr = new SpaceFamilyDataReader();
			this.accessoryType = accessoryType;
			this.treeView = tv;
			this.treeView1.Controls.Add(this.treeView);
			this.initTreeView(this.treeView);
			this.initlsvDetail(this.treeView);
			this.chooseType = chooseType;
			this.categoryName = categoryName;
			if (chooseType == "ConnectionSetting")
			{
				this.btnOk.Hide();
				base.AcceptButton = this.btnChoose;
			}
			else
			{
				this.btnChoose.Hide();
				base.AcceptButton = this.btnOk;
			}
			this.initFamilyTypeCB(this.treeView);
			if (accessoryType == "pipe")
			{
				if (beSelect)
				{
					this.Text = "选择水管阀件";
					this.groupBox1.Text = "水阀名称列表";
					this.groupBox2.Text = "水阀图示列表";
					this.btnMore.Visible = false;
					this.hygrpList.Text = "排水附件名称列表";
					this.hygrpDetail.Text = "排水附件图示列表";
					this.tvwCurrPrj.Nodes.Add("管道附件");
					this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
					this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				}
				else
				{
					this.Text = "水阀布置";
					this.groupBox1.Text = "水阀名称列表";
					this.groupBox2.Text = "水阀图示列表";
					this.btnMore.Text = "更多水阀";
					this.hygrpList.Text = "排水附件名称列表";
					this.hygrpDetail.Text = "排水附件图示列表";
					this.tvwCurrPrj.Nodes.Add("管道附件");
					this.BingTvwFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
					this.BingRecordFamilys(this.GetFamilys(BuiltInCategory.OST_PipeAccessory));
				}
			}
			this.InitlsvCurrPrjDetail(this.tvwCurrPrj);
			this.InitTabCotorl();
		}

		public DuctAccessoryForm(List<TreeView> listv, Autodesk.Revit.DB.Document doc, string accessoryType, string chooseType, string categoryName)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.sfdr = new SpaceFamilyDataReader();
			this.accessoryType = accessoryType;
			this.categoryName = categoryName;
			foreach (TreeView item in listv)
			{
				this.Listv.Add(item);
			}
			this.btnChoose.Hide();
			base.AcceptButton = this.btnOk;
			this.label1.Visible = false;
			this.textKeyWord.Visible = false;
			this.groupBox1.Location = new System.Drawing.Point(12, 96);
			this.groupBox1.Size = new Size(217, 362);
			this.groupBox3.Visible = true;
			if (accessoryType == "PipeAccessories")
			{
				this.radioPipe.Checked = true;
				return;
			}
			if (accessoryType == "GPipeAccessories")
			{
				this.radioGeishui.Checked = true;
				return;
			}
			if (accessoryType == "PPipeAccessories")
			{
				this.radioPaishui.Checked = true;
			}
		}

		private void OrderDuctValve(TreeView tv)
		{
			if ("duct" != this.accessoryType)
			{
				return;
			}
			List<string> list = new List<string>();
			list.Add("70℃矩形常闭防火阀");
			list.Add("280℃矩形常闭防火阀");
			list.Add("70℃矩形常开防火阀");
			list.Add("280℃矩形常开防火阀");
			List<TreeNode> list2 = new List<TreeNode>();
			foreach (string a in list)
			{
				foreach (object obj in tv.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (a == treeNode.Text)
					{
						list2.Add(treeNode);
						tv.Nodes.Remove(treeNode);
						break;
					}
				}
			}
			foreach (TreeNode node in list2)
			{
				tv.Nodes.Insert(0, node);
			}
			list2.Clear();
			list.Clear();
			list.Add("280℃圆形常开防火阀");
			list.Add("70℃圆形常开防火阀");
			list.Add("280℃圆形常闭防火阀");
			list.Add("70℃圆形常闭防火阀");
			foreach (string a2 in list)
			{
				foreach (object obj2 in tv.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj2;
					if (a2 == treeNode2.Text)
					{
						list2.Add(treeNode2);
						tv.Nodes.Remove(treeNode2);
						break;
					}
				}
			}
			foreach (TreeNode node2 in list2)
			{
				tv.Nodes.Add(node2);
			}
			tv.Refresh();
		}

		private void initTreeView(TreeView tv)
		{
			tv.Dock = DockStyle.Fill;
			tv.FullRowSelect = true;
			tv.AfterSelect += this.treeView_AfterSelect;
			tv.BeforeSelect += this.treeView_BeforeSelect;
			tv.NodeMouseDoubleClick += this.treeView_NodeMouseDoubleClick;
		}

		private void initlsvDetail(TreeView tv)
		{
			this.lsvDetail.Items.Clear();
			foreach (object obj in tv.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Tag is ListItem)
				{
					ListItem listItem = treeNode.Tag as ListItem;
					this.lsvDetail.LargeImageList.Images.Add(listItem.Name, Image.FromFile(listItem.Value));
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = listItem.Name;
					listViewItem.Tag = listItem;
					listViewItem.ImageKey = listItem.Name;
					this.lsvDetail.Items.Add(listViewItem);
					DuctAccessoryForm.SetListViewSpacing(this.lsvDetail, this.lsvDetail.LargeImageList.ImageSize.Width + 30, this.lsvDetail.LargeImageList.ImageSize.Height + 10);
				}
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			if (this.accessoryType != "PipeAccessories")
			{
				this.treeView1.Nodes.Clear();
				this.treeView1.Controls.Clear();
				foreach (object obj in this.treeView.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if ((treeNode.Tag as ListItem).Name.Contains(this.textKeyWord.Text.Trim()))
					{
						this.treeView1.Nodes.Add((TreeNode)treeNode.Clone());
					}
				}
				this.initTreeView(this.treeView1);
				if (this.tabHyControl.SelectedIndex == 0)
				{
					this.initlsvDetail(this.treeView1);
				}
				else
				{
					this.InitlsvCurrPrjDetail(this.tvwCurrPrj);
				}
				this.initFamilyTypeCB(this.treeView1);
			}
		}

		private void initFamilyTypeCB(TreeView tv)
		{
			string name = "";
			if ("duct" == this.accessoryType)
			{
				name = this.userSetting.GetAccessoryText();
			}
			else if ("pipe" == this.accessoryType)
			{
				name = this.userSetting.GetPipeAccessoryText();
			}
			else if ("PipeAccessories" == this.accessoryType)
			{
				name = this.userSetting.GetWaterPipeAccessoryText();
			}
			else if ("GPipeAccessories" == this.accessoryType)
			{
				name = this.userSetting.GetWaterGPipeAccessoryText();
			}
			else if ("PPipeAccessories" == this.accessoryType)
			{
				name = this.userSetting.GetWaterPPipeAccessoryText();
			}
			EquipmentCoilSetParam equipmentCoilSetText = this.userSetting.GetEquipmentCoilSetText();
			PumpCoilSetParam pumpValveText = this.userSetting.GetPumpValveText();
			if (base.AcceptButton == this.btnChoose)
			{
				if ("pipe" == this.accessoryType)
				{
					string defaultValve = equipmentCoilSetText.DefaultValve;
					this.tnSelected = TreeViewTools.GetSelectedNode(tv, defaultValve);
				}
				else if ("pump" == this.accessoryType)
				{
					string defaultValve = pumpValveText.DefaultValve;
					this.tnSelected = TreeViewTools.GetSelectedNode(tv, defaultValve);
				}
			}
			else
			{
				this.tnSelected = TreeViewTools.GetSelectedNode(tv, name);
			}
			if (this.tnSelected != null)
			{
				tv.SelectedNode = this.tnSelected;
				return;
			}
			if (tv.Nodes != null && tv.Nodes.Count > 0)
			{
				TreeNode tn = tv.Nodes[0];
				this.tnSelected = TreeViewTools.GetFirstNode(tn);
				tv.SelectedNode = this.tnSelected;
			}
		}

		protected void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Nodes == null || e.Node.Nodes.Count < 1)
			{
				this.treeView.SelectedNode = e.Node;
				if (this.btnOk.Visible)
				{
					this.btnOk_Click(null, null);
					return;
				}
				this.btnChoose_Click(null, null);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void DuctAccessoryForm_Load(object sender, EventArgs e)
		{
		}

		protected void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tnSelected != null)
			{
				this.tnSelected.BackColor = System.Drawing.Color.White;
				this.tnSelected.ForeColor = System.Drawing.Color.Black;
			}
		}

		private bool IsHaveLstViewItem(TreeNode tn)
		{
			bool result = false;
			if (this.lsvDetail.Items.Count > 0)
			{
				IEnumerator enumerator = this.lsvDetail.Items.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						if (((ListViewItem)enumerator.Current).Text == tn.Text)
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}

		protected void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			foreach (object obj in this.lsvDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Text == e.Node.Text)
				{
					listViewItem.Selected = true;
				}
				else
				{
					listViewItem.Selected = false;
				}
			}
			if (this.chooseType == "ConnectionSetting")
			{
				base.AcceptButton = this.btnChoose;
				return;
			}
			base.AcceptButton = this.btnOk;
		}

		private void lsvDetail_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (object obj in this.lsvDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Selected)
				{
					this.isHave = true;
					if (this.treeView.SelectedNode != null)
					{
						this.tnSelected = this.treeView.SelectedNode;
						this.tnSelected.BackColor = System.Drawing.Color.White;
						this.tnSelected.ForeColor = System.Drawing.Color.Black;
					}
					this.treeView.SelectedNode = TreeViewTools.GetSelectedNode(this.treeView, listViewItem.Text);
					this.tnSelected = this.treeView.SelectedNode;
					this.tnSelected.BackColor = System.Drawing.Color.DodgerBlue;
					this.tnSelected.ForeColor = System.Drawing.Color.White;
					listViewItem.EnsureVisible();
					break;
				}
			}
			if (!this.isHave && this.treeView.SelectedNode != null)
			{
				foreach (object obj2 in this.lsvDetail.Items)
				{
					ListViewItem listViewItem2 = (ListViewItem)obj2;
					if (listViewItem2.Text == this.treeView.SelectedNode.Text)
					{
						listViewItem2.Selected = true;
						listViewItem2.EnsureVisible();
						break;
					}
				}
			}
			if (this.chooseType == "ConnectionSetting")
			{
				base.AcceptButton = this.btnChoose;
				return;
			}
			base.AcceptButton = this.btnOk;
		}

		private void lsvDetail_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.btnOk.Visible)
			{
				this.btnOk_Click(null, null);
				return;
			}
			this.btnChoose_Click(null, null);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.treeView.SelectedNode == null || (this.treeView.SelectedNode.Nodes != null && this.treeView.SelectedNode.Nodes.Count >= 1))
			{
				YJKMessageBox.Show("请选择阀件");
				return;
			}
			this.UserSettingWrite();
			try
			{
				this.FamilyLoad();
				base.DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			}
		}

		private void btnChoose_Click(object sender, EventArgs e)
		{
			if (this.treeView.SelectedNode == null || (this.treeView.SelectedNode.Nodes != null && this.treeView.SelectedNode.Nodes.Count >= 1))
			{
				YJKMessageBox.Show("请选择阀件");
				return;
			}
			this.strFamilyName = this.treeView.SelectedNode.Text;
			base.DialogResult = DialogResult.OK;
		}

		private void FamilyLoad()
		{
			if (this.tabHyControl.SelectedIndex == 0)
			{
				this.GetYArchitechFamily();
				return;
			}
			this.GetCurrPrjFamily();
		}

		private void GetYArchitechFamily()
		{
			try
			{
				ListItem listItem = this.treeView.SelectedNode.Tag as ListItem;
				if (this.accessoryType == "duct")
				{
					this.fs = this.sfdr.GetFamilySymbolById(Convert.ToInt32(listItem.Tag.ToString()), this.categoryName, this.doc, BuiltInCategory.OST_DuctAccessory);
				}
				else
				{
					int id = Convert.ToInt32(listItem.Tag.ToString());
					if (this.seclected)
					{
						this.fs = this.sfdr.GetFamilySymbolByIdSubTran(id, this.categoryName, this.doc, BuiltInCategory.OST_PipeAccessory);
					}
					else
					{
						this.fs = this.sfdr.GetFamilySymbolById(id, this.categoryName, this.doc, BuiltInCategory.OST_PipeAccessory);
					}
				}
				if (this.fs == null)
				{
					throw new Exception("加载族失败");
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				MessageBox.Show("读取族库失败", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void GetCurrPrjFamily()
		{
			if (this.tvwCurrPrj.SelectedNode.Level == 2)
			{
				this.fs = (this.tvwCurrPrj.SelectedNode.Tag as FamilySymbol);
				return;
			}
			if (this.tvwCurrPrj.SelectedNode.Level == 1)
			{
				this.fs = (this.tvwCurrPrj.SelectedNode.Nodes[0].Tag as FamilySymbol);
			}
		}

		private void UserSettingWrite()
		{
			try
			{
				if (this.treeView.SelectedNode != null)
				{
					string text = this.treeView.SelectedNode.Text;
					if ("duct" == this.accessoryType)
					{
						this.userSetting.WriteAccessoryText(text);
					}
					else if ("pipe" == this.accessoryType)
					{
						this.userSetting.WritePipeAccessoryText(text);
					}
					else if ("PipeAccessories" == this.accessoryType)
					{
						this.userSetting.WriteWaterPipeAccessoryText(text);
					}
					else if ("GPipeAccessories" == this.accessoryType)
					{
						this.userSetting.WriteWaterGPipeAccessoryText(text);
					}
					else if ("PPipeAccessories" == this.accessoryType)
					{
						this.userSetting.WriteWaterPPipeAccessoryText(text);
					}
				}
				this.RecordTabContorl();
			}
			catch (Exception)
			{
			}
		}

		private void radioPipe_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioPipe.Checked)
			{
				this.accessoryType = "PipeAccessories";
				this.UpdateUI("管道附件");
			}
		}

		private void radioGeishui_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioGeishui.Checked)
			{
				this.accessoryType = "GPipeAccessories";
				this.UpdateUI("给水附件");
			}
		}

		private void radioPaishui_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioPaishui.Checked)
			{
				this.accessoryType = "PPipeAccessories";
				this.UpdateUI("排水附件");
			}
		}

		private void UpdateUI(string szText)
		{
			this.Text = szText;
			this.groupBox1.Text = szText + "名称列表";
			this.groupBox2.Text = szText + "图示列表";
			if (szText == "管道附件")
			{
				this.treeView = this.Listv[0];
			}
			else if (szText == "给水附件")
			{
				this.treeView = this.Listv[1];
			}
			else if (szText == "排水附件")
			{
				this.treeView = this.Listv[2];
			}
			this.treeView1.Controls.Clear();
			this.treeView1.Controls.Add(this.treeView);
			this.initTreeView(this.treeView);
			if (this.tabHyControl.SelectedIndex == 0)
			{
				this.initlsvDetail(this.treeView);
			}
			else
			{
				this.InitlsvCurrPrjDetail(this.tvwCurrPrj);
			}
			this.treeView1.SelectedNode = TreeViewTools.GetSelectedNode(this.treeView, "");
			this.treeView.SelectedNode = TreeViewTools.GetSelectedNode(this.treeView, "");
			this.initFamilyTypeCB(this.treeView);
		}

		private void textKeyWord_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void textKeyWord_TextChanged(object sender, EventArgs e)
		{
			this.treeView1.Nodes.Clear();
			this.treeView1.Controls.Clear();
			foreach (object obj in this.treeView.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if ((treeNode.Tag as ListItem).Name.Contains(this.textKeyWord.Text.Trim()))
				{
					this.treeView1.Nodes.Add((TreeNode)treeNode.Clone());
				}
			}
			this.initTreeView(this.treeView1);
			this.initlsvDetail(this.treeView1);
			this.initFamilyTypeCB(this.treeView1);
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			if (FamilyUtil.PathIni.ReadPath(this.doc.Application.VersionNumber) == "")
			{
				YJKMessageBox.Show("请先安装盈建科族立得。");
				return;
			}
			AccessoryMoreForm accessoryMoreForm = new AccessoryMoreForm(this.doc, this.accessoryType, this.chooseType);
			if (accessoryMoreForm.ShowDialog() == DialogResult.OK)
			{
				this.fs = accessoryMoreForm.fs;
				base.DialogResult = DialogResult.OK;
				return;
			}
		}

		private List<Family> GetFamilys(BuiltInCategory category)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(this.doc).OfClass(typeof(Family)).ToElements();
			List<Family> list = new List<Family>();
			foreach (Element element in enumerable)
			{
				Family family = (Family)element;
                if (family.FamilyCategory.Id.IntegerValue == (int)category)
				{
					list.Add(family);
				}
			}
			return list;
		}

		private void BingTvwFamilys(List<Family> familys)
		{
			this.tvwCurrPrj.Nodes[0].Nodes.Clear();
			TreeNode treeNode = this.tvwCurrPrj.Nodes[0];
			foreach (Family family in familys)
			{
				TreeNode treeNode2 = new TreeNode(family.Name)
				{
					Tag = family
				};
				this.BingTvwFamilySymbol(treeNode2, family);
				treeNode.Nodes.Add(treeNode2);
			}
			this.tvwCurrPrj.SelectedNode = treeNode;
			this.tvwCurrPrj.ExpandAll();
		}

		private void BingRecordFamilys(List<Family> familys)
		{
			this.recordCurrPrjFamilyList.Nodes.Clear();
			foreach (Family family in familys)
			{
				TreeNode treeNode = new TreeNode(family.Name)
				{
					Tag = family
				};
				this.BingTvwFamilySymbol(treeNode, family);
				this.recordCurrPrjFamilyList.Nodes.Add(treeNode);
			}
		}

		private void BingTvwFamilySymbol(TreeNode tnFamily, Family family)
		{
			foreach (ElementId elementId in family.GetSymbolIds())
			{
				FamilySymbol familySymbol = this.doc.GetElement(elementId) as FamilySymbol;
				TreeNode node = new TreeNode(familySymbol.Name)
				{
					Tag = familySymbol
				};
				tnFamily.Nodes.Add(node);
			}
		}

		private void InitlsvCurrPrjDetail(TreeView tv)
		{
			this.lsvCurrPrjDetail.Items.Clear();
			if (tv.Nodes[0].Nodes.Count == 0)
			{
				return;
			}
			foreach (object obj in tv.Nodes[0].Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Level == 1 && treeNode.Tag is Family)
				{
					Family family = treeNode.Tag as Family;
					string text = this.GetImageByFamily(family);
					if (string.IsNullOrEmpty(text) || !File.Exists(text))
					{
						text = Path.Combine(Product.FamilyImageLocation, "default.bmp");
					}
					this.lsvCurrPrjDetail.LargeImageList.Images.Add(family.Name, Image.FromFile(text));
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Text = family.Name;
					listViewItem.Tag = family;
					listViewItem.ImageKey = family.Name;
					this.lsvCurrPrjDetail.Items.Add(listViewItem);
					DuctAccessoryForm.SetListViewSpacing(this.lsvCurrPrjDetail, this.lsvCurrPrjDetail.LargeImageList.ImageSize.Width + 30, this.lsvCurrPrjDetail.LargeImageList.ImageSize.Height + 10);
				}
			}
		}

		private string GetImageByFamily(Family family)
		{
			string result;
			if (File.Exists(Path.Combine(Product.FamilyImageLocation, "DuctValve", family.Name + ".bmp")))
			{
				result = Path.Combine(Product.FamilyImageLocation, "DuctValve", family.Name + ".bmp");
			}
			else if (File.Exists(Path.Combine(Product.FamilyImageLocation, "PipeValve", family.Name + ".bmp")))
			{
				result = Path.Combine(Product.FamilyImageLocation, "PipeValve", family.Name + ".bmp");
			}
			else if (File.Exists(Path.Combine(Product.FamilyImageLocation, "Pump", family.Name + ".bmp")))
			{
				result = Path.Combine(Product.FamilyImageLocation, "Pump", family.Name + ".bmp");
			}
			else if (File.Exists(Path.Combine(Product.FamilyImageLocation, "EquipmentSanitary", family.Name + ".bmp")))
			{
				result = Path.Combine(Product.FamilyImageLocation, "EquipmentSanitary", family.Name + ".bmp");
			}
			else
			{
				result = this.CreateImageByFamily(family);
			}
			return result;
		}

		private string CreateImageByFamily(Family family)
		{
			string result = "";
			if (!File.Exists(Path.Combine(Product.FamilyImageLocation, "PipeValve", family.Name + " - 楼层平面 - 参照标高.bmp")))
			{
				Document document = this.doc.EditFamily(family);
				ImageExportOptions imageExportOptions = new ImageExportOptions();
                imageExportOptions.FitDirection = (FitDirectionType)1;
                imageExportOptions.HLRandWFViewsFileType = (ImageFileType)0;
                imageExportOptions.ImageResolution = (ImageResolution)2;
				imageExportOptions.ShouldCreateWebSite = false;
                imageExportOptions.ExportRange = (ExportRange)2;
				imageExportOptions.ZoomType = 0;
				imageExportOptions.Zoom = 100;
				imageExportOptions.FilePath = Path.Combine(Product.FamilyImageLocation, "PipeValve", family.Name);
				imageExportOptions.PixelSize = 640;
				ElementId elementId = null;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				FilteredElementIterator elementIterator = filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
				elementIterator.Reset();
				while (elementIterator.MoveNext())
				{
					Element element = elementIterator.Current;
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					if (view != null && !view.IsTemplate && (int)view.ViewType == 1 && view.Name == "参照标高")
					{
						elementId = view.Id;
						break;
					}
				}
				Transaction transaction = new Transaction(document, "deleteLines");
				transaction.Start();
				List<Element> list = filteredElementCollector.OfClass(typeof(ReferencePlane)).ToElements().ToList<Element>();
				List<Element> list2 = filteredElementCollector.OfClass(typeof(Dimension)).ToElements().ToList<Element>();
				foreach (Element elem in list)
				{
					document.DeleteElement(elem);
				}
				foreach (Element elem2 in list2)
				{
					document.DeleteElement(elem2);
				}
				document.Regenerate();
				transaction.Commit();
				if (null != elementId)
				{
					imageExportOptions.SetViewsAndSheets(new List<ElementId>
					{
						elementId
					});
					document.ExportImage(imageExportOptions);
					result = imageExportOptions.FilePath + " - 楼层平面 - 参照标高.bmp";
				}
			}
			else
			{
				result = Path.Combine(Product.FamilyImageLocation, "PipeValve", family.Name + " - 楼层平面 - 参照标高.bmp");
			}
			return result;
		}

		private void tvwCurrPrj_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.tvwCurrPrj.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
			this.tvwCurrPrj.SelectedNode.ForeColor = System.Drawing.Color.White;
			if (e.Node.Level == 0)
			{
				this.btnOk.Enabled = false;
				this.beChange = true;
			}
			else
			{
				this.btnOk.Enabled = true;
			}
			if (e.Node.Level == 0)
			{
				this.lsvCurrPrjDetail.SelectedItems.Clear();
				return;
			}
			TreeNode treeNode;
			if (e.Node.Level == 2)
			{
				treeNode = e.Node.Parent;
			}
			else
			{
				treeNode = e.Node;
			}
			foreach (object obj in this.lsvCurrPrjDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Text == treeNode.Text)
				{
					this.beChange = false;
					listViewItem.Selected = true;
				}
				else
				{
					listViewItem.Selected = false;
				}
			}
			this.beChange = true;
			if (this.chooseType == "ConnectionSetting")
			{
				base.AcceptButton = this.btnChoose;
				return;
			}
			base.AcceptButton = this.btnOk;
		}

		private void tvwCurrPrj_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwCurrPrj.SelectedNode != null)
			{
				this.tvwCurrPrj.SelectedNode.BackColor = System.Drawing.Color.White;
				this.tvwCurrPrj.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void lsvCurrPrjDetail_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (object obj in this.lsvCurrPrjDetail.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Selected)
				{
					this.have = true;
					if (this.tvwCurrPrj.SelectedNode != null)
					{
						this.tnSelected = this.tvwCurrPrj.SelectedNode;
						this.tnSelected.BackColor = System.Drawing.Color.White;
						this.tnSelected.ForeColor = System.Drawing.Color.Black;
					}
					if (this.beChange)
					{
						this.tvwCurrPrj.SelectedNode = TreeViewTools.GetSelectedNode(this.tvwCurrPrj, listViewItem.Text);
					}
					listViewItem.EnsureVisible();
					break;
				}
			}
			if (!this.have && this.tvwCurrPrj.SelectedNode != null)
			{
				string b = (this.tvwCurrPrj.SelectedNode.Level == 2) ? this.tvwCurrPrj.SelectedNode.Parent.Text : this.tvwCurrPrj.SelectedNode.Text;
				foreach (object obj2 in this.lsvDetail.Items)
				{
					ListViewItem listViewItem2 = (ListViewItem)obj2;
					if (listViewItem2.Text == b)
					{
						listViewItem2.Selected = true;
						this.beChange = true;
						listViewItem2.EnsureVisible();
						break;
					}
				}
			}
			if (this.chooseType == "ConnectionSetting")
			{
				base.AcceptButton = this.btnChoose;
				return;
			}
			base.AcceptButton = this.btnOk;
		}

		private void txtCurrPrjKeyWord_TextChanged(object sender, EventArgs e)
		{
			this.tvwCurrPrj.Nodes[0].Nodes.Clear();
			foreach (object obj in this.recordCurrPrjFamilyList.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				TreeNode node = treeNode.Clone() as TreeNode;
				if (treeNode.Text.Contains(this.txtCurrPrjKeyWord.Text.Trim()))
				{
					this.tvwCurrPrj.Nodes[0].Nodes.Add(node);
				}
			}
			this.tvwCurrPrj.ExpandAll();
			this.InitlsvCurrPrjDetail(this.tvwCurrPrj);
		}

		private void InitTabCotorl()
		{
			this.tabHyControl.SelectedIndex = (string.IsNullOrEmpty(this.userSetting.ReadValue("RecordDuctAccessoryForm", this.accessoryType)) ? 0 : int.Parse(this.userSetting.ReadValue("RecordDuctAccessoryForm", this.accessoryType)));
		}

		private void RecordTabContorl()
		{
			this.userSetting.WriteValue("RecordDuctAccessoryForm", this.accessoryType, this.tabHyControl.SelectedIndex.ToString());
		}

		private void tvwCurrPrj_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			if (e.Node.Level != 2)
			{
				return;
			}
			if (this.btnOk.Visible)
			{
				this.btnOk_Click(null, null);
				return;
			}
			this.btnChoose_Click(null, null);
		}

		private void tabHyControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.tabHyControl.SelectedIndex == 0)
			{
				this.btnOk.Enabled = true;
			}
		}

		public FamilySymbol fs;

		private Document doc;

		private TreeView treeView;

		private TreeNode tnSelected;

		private SpaceFamilyDataReader sfdr;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private string accessoryType = "duct";

		private string chooseType = "";

		public string strFamilyName = "";

		private string categoryName = "";

		private List<TreeView> Listv = new List<TreeView>();

		private bool seclected;

		private TreeView recordCurrPrjFamilyList = new TreeView();

		private bool beChange;

		private const int LVM_FIRST = 4096;

		private const int LVM_SETICONSPACING = 4149;

		private bool isHave;

		private bool have;
	}
}
